package light.head.controller.oa;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.oa.AssetsDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.Assets;
import light.head.model.AssetsAlteration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping(Url.C_ASSETS)
public class AssetsController extends SupporterController{
	private AssetsDAO asDAO;
	private OrganDAO organDAO;
	
	@RequestMapping(Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.ASSETS_INIT);
	}
	
	@RequestMapping(value = Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME, Parm.ENAME, Parm.DELETED, Parm.OC);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		if (asDAO.findByMap(organDAO.getSonCodeList(oc), map, pb) == RESULT.SUCCESS)
			request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.ASSETS_LIST);
	}

	@RequestMapping(value = Url.EDIT)
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (isNotNull(code)) {
			Assets as = asDAO.get(Integer.parseInt(code));
			List<AssetsAlteration> itemList = asDAO.findByAc(as.getCode());
			as.setItemList(itemList);
			request.setAttribute(Parm.ASSETS, as);
		}
		return new ModelAndView(Url.ASSETS_EDIT);
	}
	
	@RequestMapping(value = Url.SAVE)
	public void save(Assets as, HttpServletRequest request, HttpServletResponse response) throws Exception{
		if (as == null) {
			print(response, Msg.PARAMETER_ERROR);
			return ;
		}
		RESULT result = RESULT.ERROR;
		if (as.getCode() == null) {
			as.setOc(cu_organCode(request));
			as.setCu(cu_code(request));
			as.setCt(new Date());
			as.setDeleted(false);
			result = asDAO.add(as);
		} else {
			Assets a = asDAO.get(as.getCode());
			a.setName(as.getName());
			a.setPrice(as.getPrice());
			a.setNorms(as.getNorms());
			a.setType(as.getType());
			a.setBuyers(as.getBuyers());
			a.setRemark(as.getRemark());
			result = asDAO.upd(a);
		} if (RESULT.SUCCESS == result) {
			print(response, Msg.SUCCESS);
		} else
			print(response, Msg.ERROR);
	}
	
//	@RequestMapping(value = Url.SAVE)
//	public void comm(Assets as, HttpServletRequest request, HttpServletResponse response) throws Exception{
//		
//	}
	
	
	
	@Autowired
	public void setAsDAO(AssetsDAO asDAO) {
		this.asDAO = asDAO;
	}
	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
	
	
}
